package cn.tedu.project.oa.vehicle.controller;

import cn.tedu.project.oa.base.response.JsonResult;
import cn.tedu.project.oa.base.response.PageData;
import cn.tedu.project.oa.base.webSocket.CustomWebSocketEndpoint;
import cn.tedu.project.oa.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.project.oa.vehicle.pojo.dto.VehicleSaveParam;
import cn.tedu.project.oa.vehicle.pojo.vo.VehicleVO;
import cn.tedu.project.oa.vehicle.serveice.VehicleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import shade.com.alibaba.fastjson2.JSON;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/oa/asset/vehicle")
@Api(tags = "04.固定资产-车辆模块")
public class VehicleController {

    @Autowired
    VehicleService vehicleService;

    @ApiOperation("车辆列表查询")
    @GetMapping("select")
    public JsonResult selectVehicle (VehicleQuery vehicleQuery){
        log.debug("查询车辆列表:vehicleQuery={}",vehicleQuery);

        PageData data = vehicleService.selectVehicle(vehicleQuery);
//        List<VehicleVO> list = vehicleService.selectVehicle(vehicleQuery);

        return JsonResult.ok(data);
    }

    @ApiOperation("保存+编辑车辆")
    @PostMapping("save")
    public JsonResult saveVehicle (VehicleSaveParam vehicleSaveParam){
        log.debug("保存车辆:vehicleSaveParam={}",vehicleSaveParam);

        vehicleService.saveVehicle(vehicleSaveParam);

        return JsonResult.ok();
    }

    @ApiOperation("删除车辆")
    @PostMapping("delete/{id}/{assetCode}")
    public JsonResult deleteVehicle (@PathVariable Long id, @PathVariable String assetCode){
        log.debug("删除车辆:id={},assetCode={}",id,assetCode);

        vehicleService.deleteVehicle(id,assetCode);

        return JsonResult.ok();
    }

    //TODO被Token拦截站内信测试
    @GetMapping("addNum")
    @ResponseBody
    public String sendMessage() {
        String userId = "4";
        Map<String,String> map = new HashMap<>();
        map.put("message","新消息");
        String message = "{message : '新消息'}";

        try {
            CustomWebSocketEndpoint.sendMessageToUser(userId, JSON.toJSONString(map));
            return "发送消息成功:" + userId;
        } catch (IOException e) {
            e.printStackTrace();
            return "发送消息失败";
        }
    }


}
